package com.imiku.service;

import com.imiku.model.RoleInfo;
import com.imiku.repository.RoleInfoRepository;
import com.imiku.vo.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class RoleInfoService {

    @Autowired
    private RoleInfoRepository roleInfoRepository;

    public Page<RoleInfo> findByPage(PageVo pageVo) {
        Pageable pageable = PageRequest.of(pageVo.getPageNumber(), pageVo.getPageSize(), Sort.by(Sort.Direction.DESC, "createDate"));
        return roleInfoRepository.findAll(pageable);
    }

    public RoleInfo findByRoleId(int roleId) {
        return roleInfoRepository.findByRoleId(roleId);
    }

    public void save(RoleInfo roleInfo) {
        if (roleInfo.getRoleId() == null || roleInfo.getRoleId() < 1) {
            roleInfo.setCreateDate(new Date());
        }
        roleInfoRepository.save(roleInfo);
    }
}
